package cn.iocoder.yudao.module.of.controller.admin.documentinfo;

import cn.iocoder.yudao.module.of.controller.admin.meetingInfo.vo.MeetingInfoSaveReqVO;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.of.controller.admin.documentinfo.vo.*;
import cn.iocoder.yudao.module.of.dal.dataobject.documentinfo.DocumentInfoDO;
import cn.iocoder.yudao.module.of.service.documentinfo.DocumentInfoService;

@Tag(name = "管理后台 - 公文信息")
@RestController
@RequestMapping("/of/document-info")
@Validated
public class DocumentInfoController {

    @Resource
    private DocumentInfoService documentInfoService;

    @PostMapping("/create")
    @Operation(summary = "创建公文信息")
    @PreAuthorize("@ss.hasPermission('of:document-info:create')")
    public CommonResult<Long> createDocumentInfo(@Valid @RequestBody DocumentInfoSaveReqVO createReqVO) {
        return success(documentInfoService.createDocumentInfo(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新公文信息")
    @PreAuthorize("@ss.hasPermission('of:document-info:update')")
    public CommonResult<Boolean> updateDocumentInfo(@Valid @RequestBody DocumentInfoSaveReqVO updateReqVO) {
        documentInfoService.updateDocumentInfo(updateReqVO);
        return success(true);
    }

    @PutMapping("/send/dept")
    @Operation(summary = "分派主责部门")
    @PreAuthorize("@ss.hasPermission('of:meeting-info:update')")
    public CommonResult<Boolean> sendDept(@Valid @RequestBody DocumentInfoSaveReqVO updateReqVO) {
        documentInfoService.sendDept(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除公文信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('of:document-info:delete')")
    public CommonResult<Boolean> deleteDocumentInfo(@RequestParam("id") Long id) {
        documentInfoService.deleteDocumentInfo(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得公文信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('of:document-info:query')")
    public CommonResult<DocumentInfoRespVO> getDocumentInfo(@RequestParam("id") Long id) {
        DocumentInfoDO documentInfo = documentInfoService.getDocumentInfo(id);
        return success(BeanUtils.toBean(documentInfo, DocumentInfoRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得公文信息分页")
   // @PreAuthorize("@ss.hasPermission('of:document-info:query')")
    public CommonResult<PageResult<DocumentInfoRespVO>> getDocumentInfoPage(@Valid DocumentInfoPageReqVO pageReqVO) {
        PageResult<DocumentInfoRespVO> pageResult = documentInfoService.getDocumentInfoPage(pageReqVO);
        return success(pageResult);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出公文信息 Excel")
    @PreAuthorize("@ss.hasPermission('of:document-info:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDocumentInfoExcel(@Valid DocumentInfoPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DocumentInfoRespVO> list = documentInfoService.getDocumentInfoPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "公文信息.xls", "数据", DocumentInfoRespVO.class,
                list);
    }

}